<template>
  <div class="users">
    <el-card shadow="never">
      <template #header>
        <div class="table-header">
          <span>用户列表</span>
          <div style="display: flex; gap: 10px">
            <el-input
              v-model="keyword"
              placeholder="搜索用户名/手机号/学号"
              style="width: 250px"
              clearable
              @keyup.enter="handleSearch"
            />
            <el-button type="primary" @click="handleSearch">搜索</el-button>
          </div>
        </div>
      </template>

      <!-- 数据表格 -->
      <el-table :data="paginatedData" stripe style="width: 100%" v-loading="loading">
        <el-table-column prop="id" label="ID" width="120"></el-table-column>
        <el-table-column prop="nick" label="昵称"></el-table-column>
        <el-table-column prop="phone" label="手机号"></el-table-column>
        <el-table-column prop="studentNo" label="学号"></el-table-column>
        <el-table-column label="状态" width="120">
          <template #default="scope">
            <el-switch
              v-model="scope.row.status"
              :active-value="1"
              :inactive-value="0"
              @change="handleStatusChange(scope.row)"
            />
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150">
          <template #default="scope">
            <el-button size="small" type="danger" @click="handleDelete(scope.row)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div style="margin-top: 20px; text-align: center">
        <el-pagination
          v-model:current-page="currentPage"
          :page-size="pageSize"
          layout="prev, pager, next, total"
          :total="total"
          @current-change="handlePageChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive, computed, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { userApi } from '@/api/user'

const users = ref([])
const total = ref(0)
const dialogVisible = ref(false)
const dialogTitle = ref('用户管理')
const formRef = ref(null)
const currentPage = ref(1)
const pageSize = 10
const loading = ref(false)
const keyword = ref('')

const form = reactive({
  id: null,
  nick: '',
  studentNo: '',
  phone: '',
  status: 1
})

const paginatedData = computed(() => {
  return users.value
})

const loadData = async () => {
  loading.value = true
  try {
    const response = await userApi.listUsers(currentPage.value, pageSize, keyword.value || null)
    users.value = response.data.records || []
    total.value = response.data.total || 0
  } catch (error) {
    // 错误信息已在request拦截器中处理
  } finally {
    loading.value = false
  }
}

const handleStatusChange = async (row) => {
  try {
    await userApi.updateUserStatus(row.id, row.status)
    ElMessage.success('状态更新成功')
  } catch (error) {
    // 错误信息已在request拦截器中处理
    // 恢复原状态
    row.status = row.status === 1 ? 0 : 1
  }
}

const handleDelete = (row) => {
  ElMessageBox.confirm('确定要删除这个用户吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    try {
      await userApi.deleteUser(row.id)
      ElMessage.success('删除成功')
      loadData()
    } catch (error) {
      // 错误信息已在request拦截器中处理
    }
  })
}

const handlePageChange = (page) => {
  currentPage.value = page
  loadData()
}

const handleSearch = () => {
  currentPage.value = 1
  loadData()
}

onMounted(() => {
  loadData()
})
</script>

<style scoped>
.users {
  width: 100%;
}

.table-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

:deep(.el-card) {
  width: 100%;
}
</style>

